// HSS.CoreInstaller.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license
// http://www.opensource.org/licenses/mit-license.html
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       CoreInstaller.cs
// Author:     HSS\gbanta
// Created:    08/12/2010
// Modified:   12/06/2010
// ----------------------------------------------------------------------------
namespace HSS
{
	#region Using Directives
	using System;
	using System.ComponentModel;
	using System.Configuration.Install;
	using System.Diagnostics;
	#endregion

	#region HSS Core Installer
	/// <summary>
	/// HSS Core Installer
	/// </summary>
	[RunInstaller(true)]
	[ToolboxItemAttribute(false)]
	public partial class CoreInstaller : Installer
	{
		string AsmExSLRegKey = @"SOFTWARE\Microsoft\Microsoft SDKs\Silverlight\v4.0\AssemblyFoldersEx";
		string AsmExSL5RegKey = @"SOFTWARE\Microsoft\Microsoft SDKs\Silverlight\v5.0\AssemblyFoldersEx";
		string keyName_x86 = @"SOFTWARE\Microsoft\.NETFramework\v{0}.{1}.{2}\AssemblyFoldersEx";
		string keyName_x64 = @"SOFTWARE\Wow6432Node\Microsoft\.NETFramework\v{0}.{1}.{2}\AssemblyFoldersEx";
		string AsmExRegKey = string.Empty;

		static string CoreVersion = "0.0";

		string CommonRegKey = "HSS Core v{0} Common Libraries";
		string CommonPath = @"HighSpeed-Solutions\Core\v{0}";

		string SDKCLRegKey = "HSS Core v{0} SDK Client Libraries";
		string SDKCLPath = @"HighSpeed-Solutions SDKs\Core\v{0}\Libraries\Client";

		string SDKSLRegKey = "HSS Core v{0} SDK Silverlight Libraries";
		string SDKSLPath = @"HighSpeed-Solutions SDKs\Core\v{0}\Libraries\Silverlight";

		string SDKSL5RegKey = "HSS Core v{0} SDK Silverlight Libraries";
		string SDKSL5Path = @"HighSpeed-Solutions SDKs\Core\v{0}\Libraries\Silverlight5";

		string SDKRegKey = "HSS Core v{0} SDK Server Libraries";
		string SDKPath = @"HighSpeed-Solutions SDKs\Core\v{0}\Libraries\Server";

		/// <summary>
		/// Constructor
		/// </summary>
		public CoreInstaller()
		{
			InitializeComponent();

			if (Environment.Is64BitOperatingSystem)
				AsmExRegKey = string.Format(keyName_x64, Environment.Version.Major, Environment.Version.Minor, Environment.Version.Build);
			else
				AsmExRegKey = string.Format(keyName_x86, Environment.Version.Major, Environment.Version.Minor, Environment.Version.Build);

			CoreVersion = this.GetType().Assembly.GetName().Version.ToString(2);

			CommonRegKey = string.Format(CommonRegKey, CoreVersion);
			CommonPath = string.Format(CommonPath, CoreVersion);

			SDKCLRegKey = string.Format(SDKCLRegKey, CoreVersion);
			SDKCLPath = string.Format(SDKCLPath, CoreVersion);

			SDKSLRegKey = string.Format(SDKSLRegKey, CoreVersion);
			SDKSLPath = string.Format(SDKSLPath, CoreVersion);

			SDKSL5RegKey = string.Format(SDKSL5RegKey, CoreVersion);
			SDKSL5Path = string.Format(SDKSL5Path, CoreVersion);

			SDKRegKey = string.Format(SDKRegKey, CoreVersion);
			SDKPath = string.Format(SDKPath, CoreVersion);

			string commonFolder = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles);
			CommonPath = System.IO.Path.Combine(commonFolder, CommonPath);

			string installFolder = System.Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
			SDKCLPath = System.IO.Path.Combine(installFolder, SDKCLPath);
			SDKSLPath = System.IO.Path.Combine(installFolder, SDKSLPath);
			SDKSL5Path = System.IO.Path.Combine(installFolder, SDKSL5Path);
			SDKPath = System.IO.Path.Combine(installFolder, SDKPath);
		}

		/// <summary>
		/// Install custom actions
		/// </summary>
		/// <param name="stateSaver"></param>
		public override void Install(System.Collections.IDictionary stateSaver)
		{
			//
			// Actually performs Install actions
			// NOTE: You must call this otherwise the installation state is inconsistent
			// and will cause the overall installation process to fail
			//
			base.Install(stateSaver);

			//
			// Add Libraries to the Visual Studio AddReference Dialog
			//
			AddReferenceLookUp(AsmExRegKey, CommonRegKey, CommonPath);
			AddReferenceLookUp(AsmExRegKey, SDKRegKey, SDKPath);
			AddReferenceLookUp(AsmExRegKey, SDKCLRegKey, SDKCLPath);
			AddReferenceLookUp(AsmExSLRegKey, SDKSLRegKey, SDKSLPath);
			AddReferenceLookUp(AsmExSL5RegKey, SDKSL5RegKey, SDKSL5Path);

			//
			// Set the Overflow Policy for the HSS EventLog
			//
			try
			{
				using (EventLog el = new EventLog(this.eliCore.Log))
					el.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, 0);
			}
			catch { }

		}

		/// <summary>
		/// Rollback custom actions
		/// </summary>
		/// <param name="savedState"></param>
		public override void Rollback(System.Collections.IDictionary savedState)
		{
			//
			// Actually performs Rollback actions
			// NOTE: You must call this otherwise the installation state is inconsistent
			// and will cause the overall install process to fail
			//
			base.Rollback(savedState);

			//
			// Remove Libraries from the Visual Studio AddReference Dialog
			//
			RemoveReferenceLookUp(AsmExRegKey, CommonRegKey);
			RemoveReferenceLookUp(AsmExRegKey, SDKRegKey);
			RemoveReferenceLookUp(AsmExRegKey, SDKCLRegKey);
			RemoveReferenceLookUp(AsmExSLRegKey, SDKSLRegKey);
			RemoveReferenceLookUp(AsmExSL5RegKey, SDKSL5RegKey);
		}

		/// <summary>
		/// Uninstall custom actions
		/// </summary>
		/// <param name="savedState"></param>
		public override void Uninstall(System.Collections.IDictionary savedState)
		{
			//
			// Actually performs Uninstall actions
			// NOTE: You must call this otherwise the installation state is inconsistent
			// and will cause the overall uninstall process to fail
			//
			base.Uninstall(savedState);

			//
			// Remove Libraries from the Visual Studio AddReference Dialog
			//
			RemoveReferenceLookUp(AsmExRegKey, CommonRegKey);
			RemoveReferenceLookUp(AsmExRegKey, SDKRegKey);
			RemoveReferenceLookUp(AsmExRegKey, SDKCLRegKey);
			RemoveReferenceLookUp(AsmExSLRegKey, SDKSLRegKey);
			RemoveReferenceLookUp(AsmExSL5RegKey, SDKSL5RegKey);
		}

		private void AddReferenceLookUp(string rootKey, string subKey, string path)
		{
			Microsoft.Win32.RegistryKey root = null;
			try
			{
				root = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(rootKey, true);
				if (null != root)
				{
					Microsoft.Win32.RegistryKey assName = null;
					try
					{
						// Add
						assName = root.CreateSubKey(subKey);
						assName.SetValue("", path);
					}
					catch { }
					finally
					{
						if (null != assName)
						{
							assName.Close();
							assName = null;
						}
					}
				}
			}
			catch { }
			finally
			{
				if (null != root)
				{
					root.Close();
					root = null;
				}
			}
		}
		private void RemoveReferenceLookUp(string rootKey, string subKey)
		{
			Microsoft.Win32.RegistryKey root = null;
			try
			{
				root = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(rootKey, true);
				if (null != root)
				{
					try
					{
						// Remove
						root.DeleteSubKeyTree(subKey);
					}
					catch { }
				}
			}
			catch { }
			finally
			{
				if (null != root)
				{
					root.Close();
					root = null;
				}
			}
		}
	}
	#endregion
}